package com.viewplug.view.swap;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;
import android.widget.Toast;

import com.viewplug.R;
import com.viewplug.view.utils.ScreenUtils;
import com.viewplug.view.utils.ViewHelper;

/**
 * Created by mac_wk on 14-12-29.
 */
public class WResideView extends FrameLayout {
    public final static String TAG = "WResideView";
    private View leftView;
    private View rightView;
    private boolean first_flag = true;
    private Scroller scroller;
    private VelocityTracker vTracker;

    public WResideView(Context context) {
        this(context, null);
    }

    public WResideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public WResideView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WResideView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);
    }

    private int scrollWidth = 0;
    private int totalWidth = 0;

    private void init() {

        scroller = new Scroller(getContext());
        int width = ScreenUtils.getScreenWidth(getContext());
        totalWidth = width;
        scrollWidth = width * 3 / 5;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (first_flag) {
            first_flag = false;
            int count = getChildCount();
            if (count != 2)
                throw new IllegalStateException("child  required two views");
            leftView = getChildAt(0);
            rightView = getChildAt(1);
            if (childView != null) {
                int wi = childView.getMeasuredWidth();
                int padding = totalWidth - (totalWidth - scrollWidth) - wi;
                padding = padding * 2;
                if (padding > 0)
                    leftView.setPadding(0, 0, padding, 0);
                Log.d(TAG, "totalWidth:" + totalWidth + " scrollWidth:" + scrollWidth + " padding:" + padding + "wi " + wi);
            }


        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        leftView.layout((int) (-scrollWidth * 1.5f), 0, (int) ((-scrollWidth * 1.5f) + leftView.getMeasuredWidth()), leftView.getMeasuredHeight());
        rightView.layout(0, 0, rightView.getMeasuredWidth(), rightView.getMeasuredHeight());
    }

    private boolean first_layout = true;
    private float lastX;


    int count = 0;
    //是否抢断事件
    private boolean isInterceptEvent = false;
    private boolean isShow = false;

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        return super.dispatchTouchEvent(ev);
//    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                if (vTracker == null) {
                    vTracker = VelocityTracker.obtain();
                } else {
                    vTracker.clear();
                }
                vTracker.addMovement(event);
                super.dispatchTouchEvent(event);
                return true;
            case MotionEvent.ACTION_MOVE:
                if (!isInterceptEvent)
                    return super.dispatchTouchEvent(event);
                int movex = (int) (event.getX() - lastX);
                lastX = event.getX();
                move(movex);
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                scroller();
                releasevTracker();
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), 0);
            count = (-scroller.getCurrX());
            scale();

            postInvalidate();
        }
        super.computeScroll();
    }

    private void scroller() {
        int final_scroll = 0;
        //小于两倍就滚回去
        if (Math.abs(count) < scrollWidth / 2) {
            final_scroll = count;
            isShow = false;
            isInterceptEvent = false;
        } else {
            final_scroll = -scrollWidth + count;
            isShow = true;
            isInterceptEvent = true;
        }
        scroller.startScroll(-count, 0, final_scroll, 0);
        invalidate();

    }

    private void move(int movex) {

        count += movex;
        if (count < 0 || count > scrollWidth) {
            count -= movex;
            return;
        }

        scale();

    }

    private void scale() {
        scrollTo(-count, 0);
        float f = scrollWidth / 1000f;
        float scale_f = 1.0f - f;
        float scale = 1.0f - count / 1000f;
        float scale_left = scale_f + count / 1000f;
        ViewHelper.setScaleX(rightView, scale);
        ViewHelper.setScaleY(rightView, scale);
        ViewHelper.setScaleX(leftView, scale_left);
        ViewHelper.setScaleY(leftView, scale_left);

    }


    public boolean isShow() {
        return isShow;
    }

    public void show() {
        if (isShow) {
            scroller.startScroll(scrollWidth, 0, -scrollWidth, 0);

        } else {
            scroller.startScroll(0, 0, -scrollWidth, 0);
        }
        invalidate();
        isShow = !isShow;

    }

    private void releasevTracker() {
        if (vTracker != null) {
            vTracker.clear();
            vTracker.recycle();
            vTracker = null;
        }

    }

    public boolean isInterceptEvent() {
        return isInterceptEvent;
    }

    public void setInterceptEvent(boolean isInterceptEvent) {
        this.isInterceptEvent = isInterceptEvent;
    }

    private View childView = null;


    public void setChildView(View childView) {
        this.childView = childView;
    }
}
